package com.alipay.marketing;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayMarketingCardDeleteRequest;
import com.alipay.api.request.AlipayMarketingCardQueryRequest;
import com.alipay.api.request.AlipayMarketingCardTemplateCreateRequest;
import com.alipay.api.request.AlipayMarketingCardTemplateModifyRequest;
import com.alipay.api.request.AlipayMarketingCardTemplateQueryRequest;
import com.alipay.api.request.AlipayMarketingCardUpdateRequest;
import com.alipay.api.response.AlipayMarketingCardDeleteResponse;
import com.alipay.api.response.AlipayMarketingCardQueryResponse;
import com.alipay.api.response.AlipayMarketingCardTemplateCreateResponse;
import com.alipay.api.response.AlipayMarketingCardTemplateModifyResponse;
import com.alipay.api.response.AlipayMarketingCardTemplateQueryResponse;
import com.alipay.api.response.AlipayMarketingCardUpdateResponse;
import com.alipay.factory.AlipayAPIClientFactory;

/**
 * 会员卡相关业务调用接口
 * 
 * @author zwm
 *
 */
public class AlipayMarketingCardManage {

	public static void main(String[] args) throws AlipayApiException {
		// deleteCard();
		// updateCard();
		// queryCard();
		 //queryTemplate();
		//createCard();
		//updateCardTemplate();
		updateCardTemplate();
	}

	/**
	 * 会员卡查询
	 * 
	 * @param appAuthToken如ISV代替商户调用接口，需将商户授权后获取的app_auth_token带上；如商户自己调用，
	 *            则传null。
	 * @param bizContent
	 *            因该接口业务参数复杂，请详见接口文档
	 * @return
	 * @throws AlipayApiException
	 *             请处理异常
	 */
	public static void queryCard() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();

		AlipayMarketingCardQueryRequest request = new AlipayMarketingCardQueryRequest();
		request.putOtherTextParam("app_auth_token", null);
		String bizContent = "{" + "\"target_card_no\":\"prexupdate0000000190\","// 开卡时返回的target_card_no
				+ "\"target_card_no_type\":\"BIZ_CARD\"," + "\"card_user_info\":{"
				+ "\"user_uni_id\":\"2088702468804908\"," + "\"user_uni_id_type\":\"UID\"" + "}}";
		request.setBizContent(bizContent);
		AlipayMarketingCardQueryResponse response = alipayClient.execute(request);
		System.out.println(response.getBody());
	}

	/**
	 * 会员卡更新
	 * 
	 * @param appAuthToken如ISV代替商户调用接口，需将商户授权后获取的app_auth_token带上；如商户自己调用，
	 *            则传null。
	 * @param bizContent
	 *            因该接口业务参数复杂，请详见接口文档
	 * @return
	 * @throws AlipayApiException
	 *             请处理异常
	 */
	public static void updateCard() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayMarketingCardUpdateRequest request = new AlipayMarketingCardUpdateRequest();
		request.putOtherTextParam("app_auth_token", null);
		String bizContent = "{" + "\"target_card_no\":\"prexupdate0000000190\","// 开卡时返回的target_card_no
				+ "\"target_card_no_type\":\"BIZ_CARD\"," + "\"occur_time\":\"2016-09-27 12:12:12\","
				+ "\"card_info\":{" + "\"open_date\":\"2014-02-20 21:20:46\","
				+ "\"valid_date\":\"2020-02-20 21:20:46\"," + "\"level\":\"VIP1\"," + "\"point\":\"1880\","
				+ "\"balance\":\"1128\"}" + "}";
		request.setBizContent(bizContent);
		AlipayMarketingCardUpdateResponse response = alipayClient.execute(request);
		System.out.println(response.getBody());
	}

	/**
	 * 会员卡删除
	 * 
	 * @param appAuthToken如ISV代替商户调用接口，需将商户授权后获取的app_auth_token带上；如商户自己调用，
	 *            则传null。
	 * @param bizContent
	 *            因该接口业务参数复杂，请详见接口文档
	 * @return
	 * @throws AlipayApiException
	 *             请处理异常
	 */
	public static void deleteCard() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();

		AlipayMarketingCardDeleteRequest request = new AlipayMarketingCardDeleteRequest();
		request.putOtherTextParam("app_auth_token", null);
		String bizContent = "{" + "\"out_serial_no\":\"2016100910482088129\","// 开卡时
																				// 唯一标识
				+ "\"target_card_no\":\"prexupdate0000000190\","// 开卡时返回的target_card_no
				+ "\"target_card_no_type\":\"BIZ_CARD\"," + "\"reason_code\":\"USER_UNBUND\"" + "}";
		request.setBizContent(bizContent);
		AlipayMarketingCardDeleteResponse response = alipayClient.execute(request);
		System.out.println(response.getBody());
	}

	// 查询模板信息
	public static void queryTemplate() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayMarketingCardTemplateQueryRequest request = new AlipayMarketingCardTemplateQueryRequest();
		request.setBizContent("{" + "    \"template_id\":\"20170206000000000130129000300905\"" + "  }");
		AlipayMarketingCardTemplateQueryResponse response = alipayClient.execute(request);
		if (response.isSuccess()) {
			System.out.println("调用成功");
		} else {
			System.out.println("调用失败");
		}
		System.out.println(response.getBody());
	}

	/**
	 *  * 创建会员卡模板  * @param
	 * appAuthToken如ISV代替商户调用接口，需将商户授权后获取的app_auth_token带上；如商户自己调用，则传null。
	 *  * @param bizContent 因该接口业务参数复杂，请详见接口文档  * @return  * @throws
	 * AlipayApiException 请处理异常  
	 */
	public static void createCard() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayMarketingCardTemplateCreateRequest request = new AlipayMarketingCardTemplateCreateRequest();
		request.putOtherTextParam("app_auth_token", null);
		String bizContent = "{" + "\"request_id\":\"20170207100000023100672\","
				+ "\"card_type\":\"OUT_MEMBER_CARD\","
				+ "\"biz_no_prefix\":\"prexupdate\"," 
				+ "\"biz_no_suffix_len\":\"10\","
				+ "\"write_off_type\":\"qrcode\"," 
				+ "\"template_style_info\":{" 
				+ "\"card_show_name\":\"监利公交会员卡\","
				+ "\"logo_id\":\"C-njdD-wSuSKdSITYS7wsQAAACMAAQED\"," 
				+ "\"color\":\"rgb(55,112,179)\","
				+ "\"background_id\":\"C-njdD-wSuSKdSITYS7wsQAAACMAAQED\","
				+ "\"bg_color\":\"rgb(55,112,179)\"},"
				+ "\"template_benefit_info\":[" + "{\"title\":\"消费即折扣\","
				+ "\"benefit_desc\":[\"消费即折扣\",\"会员日7折扣\"],\"start_date\":\"2016-08-18 15:17:23\","
				+ "\"end_date\":\"2016-09-14 12:12:12\"}],"
				+ "\"column_info_list\":["
				+ "{\"code\":\"BENEFIT_INFO\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"公交付款\",\"operate_type\":\"openWeb\",\"value\":\"\"},"
				+ "{\"code\":\"BENEFIT_INFO\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"线路查询\",\"operate_type\":\"openWeb\",\"value\":\"\"},"
				+ "{\"code\":\"BENEFIT_INFO\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"我的收藏\",\"operate_type\":\"openWeb\",\"value\":\"\"},"
				+ "{\"code\":\"BENEFIT_INFO\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"乘车账单\",\"operate_type\":\"openWeb\",\"value\":\"\"},"
				+ "{\"code\":\"BENEFIT_INFO\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"线路提醒\",\"operate_type\":\"openWeb\",\"value\":\"\"}"
				+ "],"
				+ "\"field_rule_list\":[{\"field_name\":\"Balance\",\"rule_name\":\"ASSIGN_FROM_REQUEST\",\"rule_value\":\"Balance\"},"
				+ "{\"field_name\":\"Point\",\"rule_name\":\"CONST\",\"rule_value\":\"100\"}]}";

		request.setBizContent(bizContent);
		AlipayMarketingCardTemplateCreateResponse response = alipayClient.execute(request);
		System.out.println(response.getBody());

		if (response.isSuccess()) {
			System.out.println("调用成功");
		} else {
			System.out.println("调用失败");
		}
	}

	/**
	 * 会员卡模板修改
	 * 
	 * @param appAuthToken如ISV代替商户调用接口，需将商户授权后获取的app_auth_token带上；如商户自己调用，
	 *            则传null。
	 * @param bizContent
	 *            因该接口业务参数复杂，请详见接口文档
	 * @return
	 * @throws AlipayApiException
	 *             请处理异常
	 */
	public static void updateCardTemplate() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();

		AlipayMarketingCardTemplateModifyRequest request = new AlipayMarketingCardTemplateModifyRequest();
		request.putOtherTextParam("app_auth_token", null);

		String bizContent = "{"
                + "\"request_id\":\"20170912160000023100675\","
                + "\"template_id\":\"20170206000000000130129000300905\","
                + "\"biz_no_prefix\":\"prexupdate\","
                + "\"write_off_type\":\"qrcode\","
                + "\"template_style_info\":{"
                + "\"card_show_name\":\"公交会员测试卡\","
                + "\"logo_id\":\"C-njdD-wSuSKdSITYS7wsQAAACMAAQED\","
                + "\"color\":\"rgb(55,112,179)\","
                + "\"background_id\":\"C-njdD-wSuSKdSITYS7wsQAAACMAAQED\","
                + "\"bg_color\":\"rgb(55,112,179)\","
                + "\"feature_descriptions\":[\"线路：273(马鹦路邓甲村---->微湖路停车场)\",\"乘车日期：2017-02-07\"],"
                + "\"slogan\":\"付款码\","
                + "\"slogan_img_id\":\"C-njdD-wSuSKdSITYS7wsQAAACMAAQED\","
                + "\"brand_name\":\"asd\""
                + "},"
            	+ "\"column_info_list\":["
				+ "{\"code\":\"bus_pay\",\"more_info\":{\"title\":\"付款码\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"公交付款\",\"operate_type\":\"openWeb\",\"value\":\"\"},"
				+ "{\"code\":\"bus_line\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"线路查询\",\"operate_type\":\"openWeb\",\"value\":\"\"},"
				+ "{\"code\":\"bus_collect\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"我的收藏\",\"operate_type\":\"openWeb\",\"value\":\"\"},"
				+ "{\"code\":\"bus_bill\",\"more_info\":{\"title\":\"乘车账单信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\",\"descs\":[\"线路：273(马鹦路邓甲村---->微湖路停车场)\",\"乘车日期：2017-02-07\"]},\"title\":\"乘车账单\",\"operate_type\":\"openNative\",\"value\":\"\"},"
				+ "{\"code\":\"bus_remind\",\"more_info\":{\"title\":\"扩展信息\",\"url\":\"http://www.baidu.com\",\"params\":\"{}\"},\"title\":\"线路提醒\",\"operate_type\":\"openWeb\",\"value\":\"\"}"
				+ "],"
                + "\"field_rule_list\":[{\"field_name\":\"Balance\",\"rule_name\":\"ASSIGN_FROM_REQUEST\",\"rule_value\":\"Balance\"},"
                + "{\"field_name\":\"Point\",\"rule_name\":\"CONST\",\"rule_value\":\"100\"}]}";

		request.setBizContent(bizContent);
		AlipayMarketingCardTemplateModifyResponse response = alipayClient.execute(request);
		System.out.println(response.getBody());
		if (response.isSuccess()) {
			System.out.println("调用成功");
		} else {
			System.out.println("调用失败");
		}
	}
}
